// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const ExtCommands = require( './src/ExtCommands.js');
const ConstString = require( './src/ConstString.js');
const Utility = require( './src/Utility.js');
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) 
{
    InitExtension();
    
    // 注册启动插件的命令
    let startPluginCommand = vscode.commands.registerCommand('vscodeext.startPlugin', () => {
        vscode.window.showInformationMessage('插件已启动！');
    });

    // 将命令添加到插件上下文中
    context.subscriptions.push(startPluginCommand);
    
    context.subscriptions.push(vscode.commands.registerCommand(ConstString.commands.OpenProj, ExtCommands.OpenProj));
    context.subscriptions.push(vscode.commands.registerCommand(ConstString.commands.SvnCmd, ExtCommands.ExecSvnCmd));
    context.subscriptions.push(vscode.commands.registerCommand(ConstString.commands.GenLuaFuncDoc, ExtCommands.GenLuaFuncDoc));
    // context.subscriptions.push(vscode.commands.registerCommand(ConstString.commands.GenLuaTableDoc, ExtCommands.GenLuaTableDoc));
    context.subscriptions.push(vscode.commands.registerCommand(ConstString.commands.OpenBCompare, ExtCommands.OpenBCompare));
    context.subscriptions.push(vscode.commands.registerCommand(ConstString.commands.UpdateMsg, ExtCommands.UpdateMsgFromSvn));
}

// This method is called when your extension is deactivated
function deactivate() {}

// 初始化扩展
function InitExtension()
{
    // 检查是否有活动的编辑器
    if (!vscode.window.activeTextEditor) 
    {
        return; // 如果没有活动编辑器，直接返回
    }
    
    let path = vscode.window.activeTextEditor.document.fileName;
    let target = "Code_Lua";
    let targetIndex = path.indexOf(target);
    if (targetIndex === -1) 
    {
        return;
    }
    let newPath = path.substring(0, targetIndex + target.length);
    let key = Object.keys(Utility.projPathCfg).find(key => 
    {
        let cfgPath = Utility.projPathCfg[key].path;
        let newPathLowercaseDrive = newPath[0].toLowerCase() + newPath.substring(1);
        let cfgPathLowercaseDrive = cfgPath[0].toLowerCase() + cfgPath.substring(1);
        return cfgPathLowercaseDrive === newPathLowercaseDrive;
    });
    if (key === undefined) 
    {
        return;
    }
    
    let statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
    statusBarItem.text = key;
    statusBarItem.show();
}


module.exports = {
	activate,
	deactivate
}
